package com.hyzh.latte.util;

import java.util.List;
import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @author songkui
 * @since 2024/7/31 18:25
 */
public class T
{

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        testWhenComplete();

    /*    List<LocalDateTime> dateTimes = Arrays.asList(
                LocalDateTime.of(2023, 5, 15, 14, 30),
                LocalDateTime.of(2022, 12, 25, 9, 0),
                LocalDateTime.of(2024, 8, 20, 10, 0)
        );

        // Sort in descending order
        dateTimes.sort((d1, d2) -> d2.compareTo(d1));

        // Print sorted list
        dateTimes.forEach(System.out::println);*/

//        long t = 1720329312L;
//        long x = 1723194499031L;
//
//        LocalDateTime endDateTime = LocalDateTime.now();
//        LocalDateTime startDate2 = endDateTime.minusDays(5);
//       DateTime dateTime = DateUtil.date(1723512530000L);
//       String ts = dateTime.toString("yyyy-MM-dd HH:mm:ss");
//        System.out.println(ts);
//
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
//        LocalDateTime startDateTime = LocalDateTime.parse("2024-08-08 07:52:53", formatter);
//        long st = startDateTime.toEpochSecond(ZoneOffset.of("+8"));
//
//        System.out.println(startDate2.format(formatter));
//        System.out.println(st);
    }

    public static void testWhenComplete() throws ExecutionException, InterruptedException {
            CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> {
                System.out.println(Thread.currentThread() + " cf1 do something....");
                return 1/1;
            });

            CompletableFuture<Integer> cf2 = cf1.whenComplete((result, e) -> {
                System.out.println("上个任务结果：" + result);
                System.out.println("上个任务抛出异常：" + e);
                System.out.println(Thread.currentThread() + " cf2 do something....");
            });

//        //等待任务1执行完成
           System.out.println("cf1结果->" + cf1.get());
//        //等待任务2执行完成
            System.out.println("cf2结果->" + cf2.get());
    }



    public static void testCompletableFuture( ) {
        List<CompletableFuture<Integer>> futures = new ArrayList<>();

        // 模拟多个异步任务
        for (int i = 0; i < 5; i++) {
            int finalI = i;
            CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
                // 模拟计算
                try {
                    Thread.sleep(60000); // 延迟1秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return finalI * finalI; // 返回平方值
            });
            futures.add(future);
        }

        // 等待所有任务完成，并获取结果
        CompletableFuture<Void> allOf = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));

        // 获取所有结果
        CompletableFuture<List<Integer>> allResultsFuture = allOf.thenApply(v ->
                futures.stream()
                        .map(CompletableFuture::join) // 等待每个任务完成并获取结果
                        .collect(Collectors.toList())
        );

        // 处理结果
        allResultsFuture.thenAccept(results -> {
            System.out.println("All results: " + results);
        }).join(); // 等待结果处理完成
    }

}
